package com.junmeng.jdemo.retrace;

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.junmeng.jdemo.CrashHandler;
import com.junmeng.jdemo.databinding.ActivityRetraceTestBinding;
import com.junmeng.retrace.RetraceUtil;
import com.junmeng.retrace.StreamUtil;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

public class RetraceTestActivity extends AppCompatActivity {

    private ActivityRetraceTestBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityRetraceTestBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
    }

    private static String[] PERMISSIONS_STORAGE = {
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE};

    public static void verifyStoragePermissions(Activity activity) {
        // Check if we have write permission 手动请求权限
        int permission = ContextCompat.checkSelfPermission(activity,
                Manifest.permission.WRITE_EXTERNAL_STORAGE);
        if (permission != PackageManager.PERMISSION_GRANTED) {
            // We don't have permission so prompt the user
            ActivityCompat.requestPermissions(activity, PERMISSIONS_STORAGE, 111);
        }
    }

    public void onClickCrash(View view) {
        String s = null;
        s.toString();
    }

    public void onClickRetrace(View view) {
        File dir = CrashHandler.FileUtil.getCrashDir(this);
        String stace = dir + "/crash-2021-06-06_192721-1622978841279.log";
        String mapping = dir + "/mapping.txt";
        File file = new File(mapping);
        String dd = RetraceUtil.retrace(file, new File(stace));
        Toast.makeText(this, "" + dd, Toast.LENGTH_SHORT).show();
    }


    public void onClickRetrace2(View view) {
        try {
            InputStream mappingio = getAssets().open("mapping/mapping.txt");
            InputStream stackio = getAssets().open("test_crash_log.log");
            String dd = RetraceUtil.retrace(mappingio, stackio);
            Toast.makeText(this, "" + dd, Toast.LENGTH_SHORT).show();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public void onClickRetrace3(View view) {
        try {
            InputStream mappingio = getAssets().open("mapping/mapping.txt");

            String mappingString = StreamUtil.convertStreamToString(mappingio);
            if (mappingString == null) {
                return;
            }
            String dd = RetraceUtil.retrace(mappingString, getTestLog());
            Toast.makeText(this, "" + dd, Toast.LENGTH_SHORT).show();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public String getTestLog() {
        return "java.lang.IllegalStateException: Could not execute method for android:onClick\n" +
                "\tat c.c.c.q$a.onClick(:2)\n" +
                "\tat android.view.View.performClick(View.java:7216)\n" +
                "\tat android.view.View.performClickInternal(View.java:7190)\n" +
                "\tat android.view.View.access$3500(View.java:827)\n" +
                "\tat android.view.View$PerformClick.run(View.java:27663)\n" +
                "\tat android.os.Handler.handleCallback(Handler.java:900)\n" +
                "\tat android.os.Handler.dispatchMessage(Handler.java:103)\n" +
                "\tat android.os.Looper.loop(Looper.java:219)\n" +
                "\tat android.app.ActivityThread.main(ActivityThread.java:8349)\n" +
                "\tat java.lang.reflect.Method.invoke(Native Method)\n" +
                "\tat com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)\n" +
                "\tat com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1055)\n" +
                "Caused by: java.lang.reflect.InvocationTargetException\n" +
                "\tat java.lang.reflect.Method.invoke(Native Method)\n" +
                "\t... 12 more\n" +
                "Caused by: java.lang.NullPointerException: throw with null exception\n" +
                "\tat com.junmeng.jdemo.retrace.RetraceTestActivity.onClickCrash(Unknown Source:1)\n" +
                "\t... 13 more\n" +
                "java.lang.reflect.InvocationTargetException\n" +
                "\tat java.lang.reflect.Method.invoke(Native Method)\n" +
                "\tat c.c.c.q$a.onClick(:2)\n" +
                "\tat android.view.View.performClick(View.java:7216)\n" +
                "\tat android.view.View.performClickInternal(View.java:7190)\n" +
                "\tat android.view.View.access$3500(View.java:827)\n" +
                "\tat android.view.View$PerformClick.run(View.java:27663)\n" +
                "\tat android.os.Handler.handleCallback(Handler.java:900)\n" +
                "\tat android.os.Handler.dispatchMessage(Handler.java:103)\n" +
                "\tat android.os.Looper.loop(Looper.java:219)\n" +
                "\tat android.app.ActivityThread.main(ActivityThread.java:8349)\n" +
                "\tat java.lang.reflect.Method.invoke(Native Method)\n" +
                "\tat com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)\n" +
                "\tat com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1055)\n" +
                "Caused by: java.lang.NullPointerException: throw with null exception\n" +
                "\tat com.junmeng.jdemo.retrace.RetraceTestActivity.onClickCrash(Unknown Source:1)\n" +
                "\t... 13 more\n" +
                "java.lang.NullPointerException: throw with null exception\n" +
                "\tat com.junmeng.jdemo.retrace.RetraceTestActivity.onClickCrash(Unknown Source:1)\n" +
                "\tat java.lang.reflect.Method.invoke(Native Method)\n" +
                "\tat c.c.c.q$a.onClick(:2)\n" +
                "\tat android.view.View.performClick(View.java:7216)\n" +
                "\tat android.view.View.performClickInternal(View.java:7190)\n" +
                "\tat android.view.View.access$3500(View.java:827)\n" +
                "\tat android.view.View$PerformClick.run(View.java:27663)\n" +
                "\tat android.os.Handler.handleCallback(Handler.java:900)\n" +
                "\tat android.os.Handler.dispatchMessage(Handler.java:103)\n" +
                "\tat android.os.Looper.loop(Looper.java:219)\n" +
                "\tat android.app.ActivityThread.main(ActivityThread.java:8349)\n" +
                "\tat java.lang.reflect.Method.invoke(Native Method)\n" +
                "\tat com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)\n" +
                "\tat com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1055)";
    }

    public void onClickPermission(View view) {
        verifyStoragePermissions(this);
    }

}